package net.betou.cms.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.cms.entity.main.CmsSite;
import net.betou.cms.entity.main.CmsUser;
import net.betou.cms.manager.main.CmsSiteMng;
import net.betou.cms.manager.main.CmsUserMng;
import net.betou.common.web.session.SessionProvider;
import net.betou.core.manager.AuthenticationMng;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * CMS上下文信息拦截器
 * 
 * 包括登录信息、权限信息、站点信息
 */
public class FrontContextInterceptor extends HandlerInterceptorAdapter{
	@Override
	public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ) throws ServletException {
		CmsSite site = null;
		List< CmsSite > list = cmsSiteMng.getListFromCache( );
		int size = list.size( );
		if( size == 0 ){
			throw new RuntimeException( "no site record in database!" );
		}else if( size == 1 ){
			site = list.get( 0 );
		}else{
			String server = request.getServerName( );
			String alias, redirect;
			for ( CmsSite s : list ){
				// 检查域名
				if( s.getDomain( ).equals( server ) ){
					site = s;
					break;
				}
				// 检查域名别名
				alias = s.getDomainAlias( );
				if( !StringUtils.isBlank( alias ) ){
					for ( String a : StringUtils.split( alias, ',' ) ){
						if( a.equals( server ) ){
							site = s;
							break;
						}
					}
				}
				// 检查重定向
				redirect = s.getDomainRedirect( );
				if( !StringUtils.isBlank( redirect ) ){
					for ( String r : StringUtils.split( redirect, ',' ) ){
						if( r.equals( server ) ){
							try{
								response.sendRedirect( s.getUrl( ) );
							}catch( IOException e ){
								throw new RuntimeException( e );
							}
							return false;
						}
					}
				}
			}
			if( site == null ){
				throw new SiteNotFoundException( server );
			}
		}

		CmsUtils.setSite( request, site );

		CmsUser user = null;
		Integer userId = authMng.retrieveUserIdFromSession( session, request );
		if( userId != null ){
			user = cmsUserMng.findById( userId );
		}

		if( user != null ){
			CmsUtils.setUser( request, user );
		}
		return true;
	}

	private SessionProvider session;
	private CmsSiteMng cmsSiteMng;
	private CmsUserMng cmsUserMng;
	private AuthenticationMng authMng;

	@Autowired
	public void setSession( SessionProvider session ) {
		this.session = session;
	}

	@Autowired
	public void setCmsSiteMng( CmsSiteMng cmsSiteMng ) {
		this.cmsSiteMng = cmsSiteMng;
	}

	@Autowired
	public void setCmsUserMng( CmsUserMng cmsUserMng ) {
		this.cmsUserMng = cmsUserMng;
	}

	@Autowired
	public void setAuthMng( AuthenticationMng authMng ) {
		this.authMng = authMng;
	}
}